<?php

class Monitor_mainApp extends BackendApp
{
	var $_monitor_main_mod;
	var $_monitor_mod;
	var $_exattr_mod;
	function __construct()
	{
		$this->Monitor_mainApp();
	}
	function Monitor_mainApp()
	{
		parent::BackendApp();

		$this->_monitor_main_mod =& m('monitor_main');
		$this->_operator_mod =& m('operator');
		$this->_monitor_mod =& m('monitor');
	}

	/* 管理 */
	function index()
	{

		$page   =   $this->_get_page(10);   //获取分页信息
		$db=&db();
		$conditions = '1=1';
		$user_ids = $this->checkpower();
		if(!empty($user_ids)){
			$conditions.=" and m.zmm_serid in(".$user_ids.")";
		}
		$sql="SELECT m.*,o.o_name,o1.o_linkman,mb.user_name FROM zys_monitor_main m
    LEFT JOIN zys_operator o ON m.zmm_otid=o.o_tid
		LEFT JOIN zys_operator o1 ON m.zmm_consignor=o1.o_tid
    LEFT JOIN zys_member mb ON m.zmm_serid=mb.user_id where $conditions
    ORDER BY m.zmm_time DESC LIMIT ".$page['limit'];
			
			
		$sql2="select count(*)  FROM zys_monitor_main m
    LEFT JOIN zys_operator o ON m.zmm_otid=o.o_tid
		LEFT JOIN zys_operator o1 ON m.zmm_consignor=o1.o_tid
		 LEFT JOIN zys_member mb ON m.zmm_serid=mb.user_id where $conditions";

		$monitor=$db->getAll($sql);

		$monitor_count=$db->getone($sql2);
		$page['item_count']=$monitor_count;

			

		$this->_format_page($page);
		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条





		$this->assign('operator', $monitor);

		$this->assign('tabself', json_encode(array('text'=>'运营商管理')));
		$this->display('monitor_main.index.html');
	}
	//添加
	function add()
	{
		if (!IS_POST)
		{
			$db=&db();
			$sq2="SELECT o.o_name,o.o_tid  FROM zys_operator o
        where o.o_carrier=1;";//所属运营商
			$typelist=$db->getall($sq2);

			foreach ($typelist as $v){
				$carrier[$v['o_tid']]=$v['o_name'];
					
			}

			$this->assign('carrier', $carrier);


			//$this->assign('fcategory', $fcategory);
			$sq3="SELECT o.o_linkman,o.o_tid  FROM zys_operator o
    where o.o_consignor=1;";//货主
			$typelist=$db->getall($sq3);


			foreach ($typelist as $v){
				$consignor[$v['o_tid']]=$v['o_linkman'];
			}
			$this->assign('consignor', $consignor);


			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        	'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));


			$this->assign('tabself', json_encode(array('text'=>'编辑运营商')));
			$this->assign('tabback', json_encode(array('text'=>'运营商管理','url'=>'')));


			$this->display('monitor_main.form.html');
		}
		else
		{
			$data = array(
			    'zmm_name' => $_POST['zmm_name'],
                'zmm_otid'  => $_POST['zmm_otid'],
                'zmm_consignor'  => $_POST['zmm_consignor'],
                'zmm_note' => $_POST['zmm_note'],
			    'zmm_time' => gmtime(),
			    'zmm_serid' => $this->visitor->get("user_id"),



			);



			if (!$id = $this->_monitor_main_mod->add($data))
			{
				$this->show_warning('添加失败', '返回方案',    'index.php?app=monitor_main&act=add');
				return;
			}
			$this->show_message('添加成功',
                '',    'index.php?app=monitor_main&act=edit&id='.$id
				
			);
		}
	}



	//添加监控
	function addmonitor()
	{
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$this->assign('id', $id);
		if (!IS_POST)
		{
			$db=&db();
			$sql="SELECT d.d_value,d_key  FROM zys_dictionary d
        where d.d_code='m_type'";//监控类型
			$typelist=$db->getall($sql);
			foreach ($typelist as $v){
				$newtypelist[$v['d_key']]=$v['d_value'];
					
			}
			$this->assign('typelist', $newtypelist);

			$this->assign('tabself', json_encode(array('text'=>'编辑运营商')));
			$this->assign('tabback', json_encode(array('text'=>'运营商管理','url'=>'')));

			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        	'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
			$this->display('monitor.form.html');
		}
		else
		{
			$data = array(
			    'm_zmmtid' => $id,
                'm_type'  => $_POST['m_type'],
                'm_order'  => $_POST['m_order'],
                'm_lng' => $_POST['m_lng'],
			    'm_lat' => $_POST['m_lat'],
			    'm_required_service_date' => gmstr2time($_POST['m_required_service_date']),
				  'm_minspeed'  => $_POST['m_minspeed'],
                'm_maxspeed' => $_POST['m_maxspeed'],
			    'm_required_leave_date' => gmstr2time($_POST['m_required_leave_date']), 
                'm_leave_offset' => $_POST['m_leave_offset'],
			    'm_check_interval' => $_POST['m_check_interval'],
		        'm_note' => $_POST['m_note'],
			);
			if (!$id2 = $this->_monitor_mod->add($data))
			{
				$this->show_warning('添加失败', '返回方案',    'index.php?app=monitor_main&act=edit&id='.$id);
				return;
			}
			$this->show_message('添加成功','返回方案',    'index.php?app=monitor_main&act=edit&id='.$id);
		}

	}




	//删除监控方案
	function drop()
	{

		$arr = isset($_GET['id']) ? trim($_GET['id']) : '';

		$arrs=explode(',', $arr);

		if (!$this->_monitor_main_mod->drop($arrs))
		{
			$this->show_warning($this->_monitor_main_mod->get_error(),    '',   'index.php?app=monitor_main');

			return;
		}
		$this->show_message('删除成功',    '继续编辑',   'index.php?app=monitor_main');
			
	}

	//删除监控明细
	function dropmonitor()
	{
		$id = isset($_GET['id']) ? trim($_GET['id']) : 0;

		$arr = isset($_GET['selfid']) ? trim($_GET['selfid']) : '';

		$arrs=explode(',', $arr);

		if (!$this->_monitor_mod->drop($arrs))
		{
			$this->show_warning($this->_monitor_mod->get_error(),    '继续编辑',   'index.php?app=monitor_main&act=edit&id=' . $id);

			return;
		}
		$this->show_message('删除成功',    '继续编辑',   'index.php?app=monitor_main&act=edit&id=' . $id);
			
	}




	//编辑
	function edit()
	{

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);

		if(empty($id)){
			$this->show_warning('方案不存在',  '',   'index.php?app=monitor_main&act=edit&id=' . $id);
			return ;
		}


		if (!IS_POST)
		{
			$db=&db();

			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        	'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));

			$sqlnew="SELECT m.*,o.o_name,o1.o_linkman,mb.user_name FROM zys_monitor_main m
        LEFT JOIN zys_operator o ON m.zmm_otid=o.o_tid
		LEFT JOIN zys_operator o1 ON m.zmm_consignor=o1.o_tid
        LEFT JOIN zys_member mb ON m.zmm_serid=mb.user_id
        WHERE zmm_tid=".$id;


			$operator=$db->getrow($sqlnew);


			$this->assign('id', $id);

			$this->assign('operator', $operator);


			$boxtype=array();
			$boxtype['20GP']='20GP';
			$boxtype['40GP']='40GP';
			$boxtype['20RF']='20RF';
			$boxtype['40RF']='40RF';
			$boxtype['40RH']='40RH';

			$this->assign('box_options', $boxtype);


			$sql="SELECT d.d_value,m.*  FROM zys_monitor m
   LEFT JOIN zys_dictionary d ON m.m_type=d.d_key AND d.d_code='m_type'
   LEFT JOIN zys_monitor_main m1 ON m.m_zmmtid=m1.zmm_tid 
    where m1.zmm_tid=".$id;
			$fcategory=$db->getAll($sql);

			$this->assign('fcategory', $fcategory);
			$sq2="SELECT o.o_name,o.o_tid  FROM zys_operator o
    where o.o_carrier=1;";//所属运营商
			$typelist=$db->getall($sq2);

			foreach ($typelist as $v){
				$carrier[$v['o_tid']]=$v['o_name'];
					
			}

			$this->assign('carrier', $carrier);


			$this->assign('fcategory', $fcategory);
			$sq3="SELECT o.o_name,o.o_tid  FROM zys_operator o
    where o.o_consignor=1;";//货主
			$typelist=$db->getall($sq3);


			foreach ($typelist as $v){
				$consignor[$v['o_tid']]=$v['o_name'];
			}
			$this->assign('consignor', $consignor);


		 $this->assign('tabself', json_encode(array('text'=>'编辑运营商')));
		 $this->assign('tabback', json_encode(array('text'=>'运营商管理','url'=>'')));


			$this->display('monitor_main.form.html');
		}else {
			$db=&db();
			$zmm_otid=0;
			$sql="select unit from zys_member where user_id=".$this->visitor->get('user_id');
			if($db->getOne($sql)){
				$zmm_otid=$db->getOne($sql);
			}
			$data = array(
			    'zmm_name' => $_POST['zmm_name'],
                'zmm_otid'  =>$zmm_otid,
                'zmm_consignor'  => $_POST['zmm_consignor'],
                'zmm_note' => $_POST['zmm_note'],
			    'zmm_time' => $_POST['zmm_time'],

			);


			/* 保存 */
			$rows = $this->_monitor_main_mod->edit($id, $data);
			if ($this->_monitor_main_mod->has_error())
			{
				$this->show_warning($this->_monitor_main_mod->get_error(),    '继续编辑',   'index.php?app=monitor_main&act=edit&id=' . $id);
				return;
			}

			$this->show_message('编辑成功',
                '',   'index.php?app=monitor_main&act=edit&id='.$id
			);


		}
	}

	//编辑监控
	function editmonitor()
	{

		$selfid = empty($_GET['selfid']) ? 0 : intval($_GET['selfid']);

		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);



		$this->assign('id', $id);

		if (!IS_POST)
		{

			$monitor=$this->_monitor_mod->get(array(
	        'fields'   => 'this.*',
	        'conditions'  => 'm_tid='.$selfid,
			));
	   
			$this->assign('monitor', $monitor);

				/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        	'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));

			$db=&db();
			$sql="SELECT d.d_value,d_key  FROM zys_dictionary d
        where d.d_code='m_type'";//监控类型
			$typelist=$db->getall($sql);

			foreach ($typelist as $v){
				$newtypelist[$v['d_key']]=$v['d_value'];
					
			}

			$this->assign('typelist', $newtypelist);

			$this->assign('tabself', json_encode(array('text'=>'编辑运营商')));
			$this->assign('tabback', json_encode(array('text'=>'运营商管理','url'=>'')));


			$this->display('monitor.form.html');
		}else {
		
	
			
			$data = array(
			    'm_zmmtid' => $id,
                'm_type'  => $_POST['m_type'],
                'm_order'  => $_POST['m_order'],
                'm_lng' => $_POST['m_lng'],
			    'm_lat' => $_POST['m_lat'],
			    'm_required_service_date' => gmstr2time($_POST['m_required_service_date']),
				  'm_minspeed'  => $_POST['m_minspeed'],
                'm_maxspeed' => $_POST['m_maxspeed'],
			    'm_required_leave_date' => gmstr2time($_POST['m_required_leave_date']), 
                'm_leave_offset' => $_POST['m_leave_offset'],
			    'm_check_interval' => $_POST['m_check_interval'],
		        'm_note' => $_POST['m_note'],
			);

			/* 保存 */
			$rows = $this->_monitor_mod->edit($selfid, $data);
			if ($this->_monitor_mod->has_error())
			{
				$this->show_warning($this->_monitor_mod->get_error() , '',   'index.php?app=monitor_main&act=editmonitor&id=' . $id.'&selfid='.$rows);
				return;
			}

			$this->show_message('编辑成功',
                '',   'index.php?app=monitor_main&act=editmonitor&id=' . $id.'&selfid='.$selfid
			);

		}

	}

}